package com.cgsd.heyu.ad;

import android.app.Activity;
import android.view.View;
import android.widget.FrameLayout;

import com.android.library.utils.BeanUtils;
import com.apkfuns.logutils.LogUtils;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTSplashAd;
import com.cgsd.heyu.api.AppConfig;
import com.cgsd.heyu.entity.AdContentEntity;

import java.lang.ref.WeakReference;

/**
 * @author xiaoxing
 * Description 开屏广告
 * CreateData: 2020/11/24
 */
public class AdSplash {
    /**
     * 开屏广告加载超时时间,建议大于3000,这里为了冷启动第一次加载到广告并且展示,示例设置了3000ms
     */
    private static final int AD_TIME_OUT = 3000;
    private TTAdNative mTTAdNative;

    private AdContentEntity adContentEntity;
    private WeakReference<Activity> weakReference;
    private AdListener adListener;
    private int width = 1080;
    private int height = 1920;
    /**
     * 布局
     */
    private FrameLayout mFrameLayout;


    public AdSplash(Activity activity, AdContentEntity entity, FrameLayout mFragment, AdListener adListener){
        this.adListener = adListener;
        this.mFrameLayout = mFragment;
        weakReference = new WeakReference<>(activity);
        this.adContentEntity = entity;

        if (BeanUtils.isEmpty(adContentEntity)) {
            return;
        }
        if (AdContentEntity.CHUANSHANJIA.equals(adContentEntity.platformType)) {
            mTTAdNative = TTAdSdk.getAdManager().createAdNative(weakReference.get());;
        }
    }

    /**
     * 设置布局
     * @param mFrameLayout
     */
    public void setFrameLayout(FrameLayout mFrameLayout,int width,int height){
        this.mFrameLayout = mFrameLayout;
        this.width = width;
        this.height = height;
    }

    /**
     * 加载广告
     */
    public void loadAd(){
        if (AdContentEntity.GUANGDIANTONG.equals(adContentEntity.platformType)) {
            adRewardVideoG();
        } else {
            adRewardVideoC();
        }
    }

    /**
     * 加载广点通开屏甲短视频广搞
     */
    private void adRewardVideoG(){

    }

    /**
     * 加载穿山开屏甲短视频广搞
     */
    private void adRewardVideoC(){
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(adContentEntity.advertisingId)
                .setExpressViewAcceptedSize(width, height)
                .setUserID(AppConfig.AD_USER_ID)
                .build();
        mTTAdNative.loadSplashAd(adSlot, new TTAdNative.SplashAdListener() {
            @Override
            public void onError(int i, String s) {
                LogUtils.e("onError loadInteractionExpressAd"+",message="+s+",code="+i);
                if (adListener != null){
                    adListener.onFail();
                }
            }

            @Override
            public void onTimeout() {
                if (adListener != null){
                    adListener.onFail();
                }
            }

            @Override
            public void onSplashAdLoad(TTSplashAd ttSplashAd) {
                if (adListener != null){
                    adListener.onSuccess();
                }
                //获取SplashView
                View view = ttSplashAd.getSplashView();
                if (mFrameLayout != null && !weakReference.get().isFinishing()) {
                    mFrameLayout.removeAllViews();
                    //把SplashView 添加到ViewGroup中,注意开屏广告view：width =屏幕宽；height >=75%屏幕高
                    mFrameLayout.addView(view);
                }
                ttSplashAd.setSplashInteractionListener(new TTSplashAd.AdInteractionListener() {
                    @Override
                    public void onAdClicked(View view, int i) {

                    }

                    @Override
                    public void onAdShow(View view, int i) {

                    }

                    @Override
                    public void onAdSkip() {
                        //广告倒计时结束
                        if (adListener != null){
                            adListener.onVideoComplete();
                        }
                    }

                    @Override
                    public void onAdTimeOver() {
                        //广告倒计时结束
                        if (adListener != null){
                            adListener.onVideoComplete();
                        }
                    }
                });
            }
        },AD_TIME_OUT);
    }
}
